package com.fjwt.gz.proxy.ctrl.anon;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.aop.MethodLog;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.SmsConfig;
import com.fjwt.gz.db.entity.SysUser;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.SysConfigService;
import com.fjwt.gz.service.impl.SysUserAuthService;
import com.fjwt.gz.service.impl.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/*
* 登录密码接口
*
* @author xiaoyu
* @site https://pt.jdyd.xyz/
* @date 2021/6/8 17:09
*/
@RestController
@RequestMapping("/api/anon/cipher")
@Slf4j
public class RetrieveController extends CommonCtrl {

	@Autowired private SysUserService sysUserService;
	@Autowired private SysUserAuthService sysUserAuthService;
	@Autowired private SysConfigService sysConfigService;

	/** 密码找回接口  **/
	@RequestMapping(value = "/retrieve", method = RequestMethod.POST)
	@MethodLog(remark = "密码找回")
	public ApiRes retrieve() throws BizException {

		// 手机号, 已做base64处理
		String phone = Base64.decodeStr(getValStringRequired("phone"));
		// 验证码,  已做base64处理
		String code = Base64.decodeStr(getValStringRequired("code"));
		//新密码 password,  已做base64处理
        String newPwd = Base64.decodeStr(getValStringRequired("newPwd"));
		log.info("机构端密码找回接口，请求地址：/api/anon/cipher/retrieve 请求参数：手机号: {}, 验证码: {}, 新密码: {}", phone, code, newPwd);
        String codeJsonStr = RedisUtil.getString(CS.getCacheKeySmsCode(CS.SYS_ROLE_TYPE.PROXY, phone));
        if (StringUtils.isEmpty(codeJsonStr)) {
			throw new BizException("验证码已过期，请重新点击发送验证码！");
		}
		JSONObject codeJson = JSONObject.parseObject(codeJsonStr);
		String cacheCode = codeJson.getString("code");
		if(StringUtils.isEmpty(cacheCode) || !cacheCode.equalsIgnoreCase(code)){
            throw new BizException("验证码错误！");
        }
		SysUser dbUser = sysUserService.getOne(SysUser.gw().eq(SysUser::getTelphone, phone).eq(SysUser::getSysType, CS.SYS_ROLE_TYPE.PROXY));
		if (dbUser == null) {
			throw new BizException("用户不存在");
		}
		// 更新用户登录密码
		sysUserAuthService.resetAuthInfo(dbUser.getSysUserId(), null, null, true, newPwd, CS.SYS_ROLE_TYPE.PROXY);
		// TODO mq更新用户密码

		return ApiRes.ok();
	}

	/** 发送短信验证码  **/
	@RequestMapping(value = "/smsCode", method = RequestMethod.POST)
	public ApiRes smsCode() throws BizException {
		// 验证参数 手机号, 已做base64处理
		String phone = getValStringRequired("phone");
		log.info("机构端发送短信验证码接口，请求地址：/api/anon/cipher/smsCode 请求参数:手机号: {}", phone);
		if(!SmsConfig.checkMobileNumber(phone)) {
			throw new BizException("请输入有效的手机号码！");
		}

		SysUser dbUser = sysUserService.getOne(SysUser.gw().eq(SysUser::getTelphone, phone).eq(SysUser::getSysType, CS.SYS_ROLE_TYPE.PROXY));
		if (dbUser == null) {
			throw new BizException("用户不存在");
		}
		sendSmsCode(phone, SmsConfig.TYPE_RETRIEVE);

		return ApiRes.ok();
	}

	/** 获取密码校验规则正则  **/
	@RequestMapping(value = "/pwdRulesRegexp", method = RequestMethod.GET)
	public ApiRes getPwdRulesRegexp() throws BizException {
		return ApiRes.ok(sysConfigService.getDBSecurityConfig().getPasswordRegexp());
	}

}
